package com.group.activity.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.group.activity.entity.GroupPeoplerecordEntity;
import com.group.activity.entity.GroupShoprecordEntity;
import com.group.activity.service.GroupPeoplerecordService;
import com.group.activity.service.GroupShoprecordService;
import com.group.common.vo.ShopUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


import com.group.activity.dao.PuzzleListDao;
import com.group.activity.entity.PuzzleListEntity;
import com.group.activity.service.PuzzleListService;

import javax.servlet.http.HttpSession;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.List;


@Service("puzzleListService")
public class PuzzleListServiceImpl extends ServiceImpl<PuzzleListDao, PuzzleListEntity> implements PuzzleListService {

    @Autowired
    GroupPeoplerecordService groupPeoplerecordService;
    @Autowired
    GroupShoprecordService groupShoprecordService;

    @Override
    public void addGroupbuy(PuzzleListEntity puzzleListEntity, HttpSession session) {
        /*
        根据session获取当前登录信息
        */
        Object userlogin = session.getAttribute("loginUser");
        System.out.println(userlogin);
        String s = JSON.toJSONString(userlogin);
        System.out.println(s);
        ShopUserVo shopUserEntity = JSON.parseObject(s, new TypeReference<ShopUserVo>() {
        });
        System.out.println("输出......................."+s);
        Integer userId = shopUserEntity.getUserId();
        puzzleListEntity.setHeadId(userId);
        String userName = shopUserEntity.getUserName();
        puzzleListEntity.setHeadName(userName);
        String userImg = shopUserEntity.getUserImg();
        puzzleListEntity.setHeadPortrait(userImg);
        puzzleListEntity.setGroupSpuStatus(2);
        puzzleListEntity.setGroupStatus(1);
        puzzleListEntity.setGroupCount(1);
        puzzleListEntity.setGroupIssued(1);
        baseMapper.insert(puzzleListEntity);
        //把团长创建得拼团信息加入中间表
        GroupPeoplerecordEntity groupPeoplerecordEntity = new GroupPeoplerecordEntity();
        groupPeoplerecordEntity.setGroupShopId(puzzleListEntity.getPuzzleListId());
        groupPeoplerecordEntity.setGroupShopName(puzzleListEntity.getGroupName());
        groupPeoplerecordEntity.setGroupUserId(puzzleListEntity.getHeadId());
        groupPeoplerecordEntity.setGroupUserName(puzzleListEntity.getHeadName());
        groupPeoplerecordService.save(groupPeoplerecordEntity);
    }

    @Override
    public PuzzleListEntity groupshoplist(Integer groupGoodsId) {
        //根据商品得id查询这个商品得信息
        return baseMapper.selectOne(new QueryWrapper<PuzzleListEntity>().eq("group_goods_id",groupGoodsId));
    }

    @Override
    public void updateid(int i) {
        baseMapper.updateid(i);
    }

    @Override
    public void updateshopid(Integer shopid) {
        baseMapper.updateshopid(shopid);
    }

    @Override
    public PuzzleListEntity groupshopsuccess() {
        //获取信息
      return   baseMapper.groupshopsuccess();
    }

    @Override
    public void groupshopshop(PuzzleListEntity puzzleListEntity) {
        //查询信息
        PuzzleListEntity groupshopsuccess = baseMapper.groupshopsuccess();
        //获取价格
        BigDecimal groupPrice = groupshopsuccess.getGroupPrice();
        //获取人数
        Integer groupCount = groupshopsuccess.getGroupCount();
        //获取当前id
        Integer puzzleListId = groupshopsuccess.getPuzzleListId();
        //转换类型
        BigDecimal bigDecimal = new BigDecimal(groupCount);
        //Decimal 相乘
        BigDecimal multiply = groupPrice.multiply(bigDecimal);
        BigDecimal bigDecimal1 = new BigDecimal("50");
        System.out.println(bigDecimal1);
        //总金额除以提成比率
        BigDecimal decimal = multiply.divide(bigDecimal1);
        //赋值
        puzzleListEntity.setGroupCommission(decimal);
        puzzleListEntity.setGroupIssued(0);
        //修改
        baseMapper.update(puzzleListEntity,new QueryWrapper<PuzzleListEntity>().eq("puzzle_list_id",puzzleListId));
    }

}